﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;

namespace 异步编程
{
    class Program
    {
        static void Main(string[] args)
        {
            //Task.FromResult(3);
            //DoSomethingAsync();
            IList<string> entityRows = new List<string>();
            entityRows.Add("http://www.cnblogs.com/");
            var s = DownloadAllAsync(entityRows);
        }

       public static async Task DoSomethingAsync()
        {
            int val = 13;
            await Task.Delay(TimeSpan.FromSeconds(1));
            val *= 2;
            await Task.Delay(TimeSpan.FromSeconds(1));
        }

       public static async Task<string> DownloadAllAsync(IEnumerable<string> urls)
       {
           var httpclient = new HttpClient();

           var downloads = urls.Select(url => httpclient.GetStringAsync(url));

           Task<string>[] downloadTasks = downloads.ToArray();

           string[] htmlpages = await Task.WhenAll(downloadTasks);

           return string.Concat(htmlpages);

       }
    }
}
